//
//  carDetailViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/17.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "carDetailViewController.h"
#import "carsDetailModel.h"
#import "UILabel+LeftTopAlign.h"
#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height
#define WSColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
@interface carDetailViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

@property(nonatomic, retain)UIScrollView *myScrollV;
@property(nonatomic, retain)NSMutableArray *myArray;
@property(nonatomic, retain)carsDetailModel *model;
@property(nonatomic, assign)BOOL myCompare;

@property(nonatomic, retain)UILabel *numLabel;
@property(nonatomic, retain)UILabel *titleLabel;
@property(nonatomic, retain)UILabel *myLabel;
@property(nonatomic, retain)UIButton *replyButton;
@property(nonatomic, retain)UILabel *replyLabel;
@end

@implementation carDetailViewController

-(void)dealloc{
    [_replyLabel release];
    [_numLabel release];
    [_titleLabel release];
    [_myLabel release];
    [_model release];
    [_myScrollV release];
    [_myArray release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.myArray = [NSMutableArray array];
    
    // Do any additional setup after loading the view.
    //NSLog(@"%@",self.type);
    
    if ([self.type isEqualToString:@"10"]) {
        [self getData];
        self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
        self.tabBarController.tabBar.hidden = YES;
        self.view.backgroundColor = [UIColor blackColor];
        self.myScrollV = [[UIScrollView alloc] init];
        self.myScrollV.backgroundColor = [UIColor whiteColor];
        [self.view addSubview:self.myScrollV];
         self.myScrollV.pagingEnabled = YES;
        self.myScrollV.delegate= self;
        self.myScrollV.frame = CGRectMake(0, HEIGHT / 4, WIDTH, HEIGHT / 8 *3);
        [_myScrollV release];
       [self topView];
        [self bottomView];
        self.myCompare = YES;
       UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [self.view addGestureRecognizer:tap];
        [tap release];
       
    }else{
        //加载H5网页
        UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
        webView.scalesPageToFit = YES;
        webView.delegate = self;
        [webView setOpaque:NO];//opaque是不透明的意思
        [webView setScalesPageToFit:YES];//自动缩放以适应屏幕
        NSString *string = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov5.0.0/content/news/newscontent-n%@-t0.json",self.ID];
        NSURL *url = [NSURL URLWithString:string];
        NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest
        [webView loadRequest:request];//加载
        [self.view addSubview:webView];
        [webView release];
    }
    
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSMutableArray *array = [NSMutableArray arrayWithArray:self.myArray];
    NSInteger  x = scrollView.contentOffset.x / WIDTH ;
    carsDetailModel *model = [[carsDetailModel alloc] init];
    model = [array objectAtIndex:x];
    self.myLabel.text = model.descriptions;
    self.titleLabel.text = self.str;
    self.numLabel.text = [NSString stringWithFormat:@"%d/%lu",x+1,(unsigned long)self.myArray.count];
}


-(void)getData{
    [ZYNetworkHandle GetDataWithURL:[NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov5.0.0/news/newsdetailpicarticle-pm2-nid%@.json",self.ID] dic:nil success:^(id responseObject) {
        NSDictionary *resultDic = [responseObject objectForKey:@"result"];
        NSMutableArray *images = [resultDic objectForKey:@"images"];
        self.replyLabel.text = [NSString stringWithFormat:@"%@",[resultDic objectForKey:@"replycount"]];
        for (NSDictionary *dic in images) {
            self.model  = [[carsDetailModel alloc] init];
            [self.model setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:self.model];
            [_model release];
        }
        //NSLog(@"%lu",self.myArray.count);
        for (NSInteger i = 0; i < self.myArray.count; i++) {
            self.model = [self.myArray objectAtIndex:i];
            //NSLog(@"%@",self.model.imgurl);
            UIImageView *imageV = [[UIImageView alloc] init];
            imageV.frame = CGRectMake(WIDTH * i  ,-64, WIDTH, HEIGHT / 8 * 3);
            [imageV sd_setImageWithURL:[NSURL URLWithString:self.model.imgurl] placeholderImage:[UIImage imageNamed:@"NOImage"]];
            [self.myScrollV addSubview:imageV];
            imageV.userInteractionEnabled = YES;
            [imageV release];
        }
        carsDetailModel *model = [[carsDetailModel alloc] init];
        model = [self.myArray objectAtIndex:0];
        self.myLabel.text = model.descriptions;
        self.titleLabel.text = self.str;
        self.numLabel.text = [NSString stringWithFormat:@"1/%lu",self.myArray.count];
        self.myScrollV.contentSize = CGSizeMake(WIDTH * self.myArray.count, 0);
    } filed:^(NSError *error) {
    }];
}


-(void)topView{
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fx"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(item1Action)];
    item1.tintColor = [UIColor whiteColor];
    
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"sc_n"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(item2Action)];
    item2.tintColor = [UIColor whiteColor];
    
     UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"xz"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(item3Action)];
    item3.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItems = @[item1,item2,item3];
}

-(void)bottomView{
    UIView *bottomV = [[UIView alloc] init];
    bottomV.backgroundColor = [UIColor blackColor];
    bottomV.alpha = 0.5;
    [self.view addSubview:bottomV];
    bottomV.tag = 100001;
    [bottomV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(self.myScrollV.mas_bottom).offset(-15);
        make.bottom.mas_equalTo(0);
    }];
    self.numLabel = [UILabel new];
    self.numLabel.backgroundColor = [UIColor clearColor];
    [bottomV addSubview:self.numLabel];
    self.numLabel.textColor = [UIColor lightGrayColor];
    self.titleLabel = [UILabel new];
    self.titleLabel.backgroundColor = [UIColor clearColor];
    [bottomV addSubview:self.titleLabel];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.myLabel = [UILabel new];
    self.myLabel.backgroundColor = [UIColor clearColor];
    [self.myLabel sizeToFit];
    self.myLabel.numberOfLines = 0;
    [self.myLabel UILabelLeftTopAlign];
    self.myLabel.textColor = [UIColor lightGrayColor];
    self.myLabel.font = [UIFont systemFontOfSize:15];
    [bottomV addSubview:self.myLabel];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(15);
        make.height.mas_equalTo(25);
        make.width.mas_equalTo(50);
        
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.numLabel);
        make.top.mas_equalTo(self.numLabel.mas_bottom).offset(15);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(WIDTH - 40);
    }];
    [self.myLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH - 30);
        make.height.mas_equalTo(100);
    }];
    
    self.replyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.replyButton.backgroundColor = [UIColor lightGrayColor];
    [bottomV addSubview:self.replyButton];
    [self.replyButton setTitle:@" 发表评论" forState:UIControlStateNormal];
    self.replyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.replyButton addTarget:self action:@selector(replyBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.replyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.numLabel);
        make.right.mas_equalTo(-100);
        make.bottom.mas_equalTo(-10);
        make.height.mas_equalTo(30);
        
    }];
    UIImageView *imageV = [UIImageView new];
    imageV.image = [UIImage imageNamed:@"pl"];
    [bottomV addSubview:imageV];
    self.replyLabel = [UILabel new];
    self.replyLabel.backgroundColor = [UIColor clearColor];
    self.replyLabel.textColor = [UIColor whiteColor];
    [bottomV addSubview:self.replyLabel];
    
    [imageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-15);
        make.left.mas_equalTo(self.replyButton.mas_right).offset(20);
        make.height.mas_equalTo(20);
    }];
    [self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageV);
        make.bottom.mas_equalTo(imageV);
        make.left.mas_equalTo(imageV.mas_right).offset(5);
        make.right.mas_equalTo(-20);
    }];
    
}

//轻拍方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
    NSLog(@"轻拍");
    if (self.myCompare == YES) {
       NSLog(@"1");
      UIView *View = [self.view viewWithTag:100001];
        [UIView animateWithDuration:0.5f animations:^{
            View.alpha = 0;
            self.navigationController.navigationBar.alpha = 0;
            
        }];
        //self.navigationController.navigationBar.alpha = 0;
        self.myCompare = NO;
    }else{
        UIView *View = [self.view viewWithTag:100001];
        NSLog(@"2");
        [UIView animateWithDuration:0.5f animations:^{
            View.alpha = 0.5;
            self.navigationController.navigationBar.alpha = 1;
        }];
        self.myCompare = YES;
        
    }
}



//所有按钮的方法
-(void)replyBtnAction{
    NSLog(@"评论");
    
    
}
-(void)item1Action{
    NSLog(@"1");
}
-(void)item2Action{
    NSLog(@"2");
}
-(void)item3Action{
    NSLog(@"3");
}

-(void)leftAction{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.barTintColor = WSColor(120, 163, 200);
    [self.navigationController popViewControllerAnimated:YES];
}









@end
